package org.example;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataOutputStream;
import java.io.*;
import java.net.URI;
import java.util.Scanner;

public class HDFSUploader {
    private static final String HDFS_URI = "hdfs://192.168.104.165:9000"; // 替换为Hadoop集群地址
    private static FileSystem fs;
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", HDFS_URI);
        fs = FileSystem.get(URI.create(HDFS_URI), conf);

        System.out.println("请输入要上传的文件路径:");
        String localFilePath = scanner.nextLine();
        System.out.println("请输入HDFS目标路径:");
        String hdfsFilePath = scanner.nextLine();

        uploadFile(localFilePath, hdfsFilePath);
    }

    private static void uploadFile(String localFilePath, String hdfsFilePath) throws IOException {
        Path localPath = new Path(localFilePath);
        Path hdfsPath = new Path(hdfsFilePath);

        if (fs.exists(hdfsPath)) {
            System.out.println("文件已存在。请选择操作：1) 追加 2) 覆盖");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 清除换行符
            if (choice == 1) {
                appendToFile(localPath, hdfsPath);
            } else if (choice == 2) {
                fs.copyFromLocalFile(false, true, localPath, hdfsPath);
                System.out.println("文件已覆盖。");
            } else {
                System.out.println("无效的操作。");
            }
        } else {
            fs.copyFromLocalFile(false, false, localPath, hdfsPath);
            System.out.println("文件已上传。");
        }
    }

    private static void appendToFile(Path localPath, Path hdfsPath) throws IOException {
        FSDataOutputStream out = fs.append(hdfsPath);
        InputStream in = new BufferedInputStream(new FileInputStream(localPath.toString()));
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) > 0) {
            out.write(buffer, 0, bytesRead);
        }
        in.close();
        out.close();
        System.out.println("文件已追加。");
    }
}
